//
//  CWMap.m
//  CWDevelop
//
//  Created by LittoCats on 7/22/14.
//  Copyright (c) 2014 Littocats. All rights reserved.
//

#import "CWMap.h"
#import "CWLocation.h"

#import <MapKit/MapKit.h>

@interface CWMap ()

@property (nonatomic, strong) CWLocation *userLocation;

@end

@implementation CWMap

+ (CWMap *)shareInstance
{
    static CWMap *map = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        map = [[CWMap alloc] init];
    });
    return map;
}

+ (void)updateUserLocationWithHandler:(void (^)(NSDictionary *))handler options:(CWCoordinateOptions)options
{
    if (!handler) return;
    if ([NSDate timeIntervalSinceReferenceDate] - [[self shareInstance].userLocation.timestamp timeIntervalSinceReferenceDate] <= 600)
        handler([[self shareInstance].userLocation locationInfoWithOptions:options]);
    else
        [CWLocation updateUserLocationWithCompletionHandler:^(NSArray *locations) {
            //locations 中的坐标为ＧＰＳ坐标，即 WGS84 坐标
            [self shareInstance].userLocation = [CWLocation locationWithWGSLocation:[locations lastObject]];
            handler([[self shareInstance].userLocation locationInfoWithOptions:options]);
        }];
}

+ (void)distanceFromUserLocationTo:(id)locationInfo handler:(void (^)(id))handler
{
    [self updateUserLocationWithHandler:^(NSDictionary *userLocationInfo) {
       if (handler) handler([self distanceFrom:userLocationInfo to:locationInfo]);
    } options:CWCoordinateOptionsGCJ];
}
+ (id)distanceFrom:(id)from to:(id)to
{
    if ([from isKindOfClass:[NSDictionary class]] && [to isKindOfClass:[NSDictionary class]]) {
        return @([[CWLocation locationWithLocationInfo:from] distanceFromLocation:[CWLocation locationWithLocationInfo:to]]);
    }else if ([from isKindOfClass:[NSArray class]] && [to isKindOfClass:[NSArray class]]){
        NSMutableArray *result_row = [NSMutableArray new];
        for (NSDictionary *location in from) {
            [result_row addObject:[self distanceFrom:location to:to]];
        }
        return result_row;
    }else{
        NSArray *row = [from isKindOfClass:[NSArray class]] ? from : [to isKindOfClass:[NSArray class]] ? to : nil;
        NSDictionary *colum = [from isKindOfClass:[NSDictionary class]] ? from : [to isKindOfClass:[NSDictionary class]] ? to : nil;

        NSMutableArray *result = [NSMutableArray new];
        if (!row || !to) return result;
        for (NSDictionary *location in row) {
            [result addObject:[self distanceFrom:location to:colum]];
        }
        return result;
    }
}

#pragma mark- private method
+ (CLLocation *)locationWithDictionary:(NSDictionary *)location
{
    CLLocationCoordinate2D coordinate ;
    coordinate.latitude = [location[@"latitude"] doubleValue];
    coordinate.longitude = [location[@"longitude"] doubleValue];
    
    CLLocation *result = [[CLLocation alloc] initWithCoordinate:coordinate altitude:[location[@"altitude"] doubleValue] horizontalAccuracy:[location[@"horizontalAccuracy"] doubleValue] verticalAccuracy:[location[@"verticalAccuracy"] doubleValue] course:[location[@"course"] doubleValue] speed:[location[@"speed"] doubleValue] timestamp:[NSDate dateWithTimeIntervalSinceReferenceDate:[location[@"timestamp"] doubleValue]]];
    return result;
}
@end
